//
//  PasswordResetViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/5/8.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "PasswordResetViewController.h"
#import <BKKit/UIViewController+x.h>
#import "User.h"
@interface PasswordResetViewController ()

@end

@implementation PasswordResetViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(cancel:)];
}
- (void)cancel:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)submit:(id)sender{
    NSArray *checkConf = @[
    @{
        @"field":self.passwordTextField,
        @"name":NSLocalizedString(@"password_new", @"标签"),
        @"min-len":@6,
        @"max-len":@20
        }
    ];
    BOOL ret = checkInput(checkConf, ^(NSString *errorMsg) {
        [BKIndicator showMessageAndFadeOut:errorMsg completion:nil];
    });
    if (!ret) {
        return;
    }
    if (![self.passwordTextField.text isEqualToString:self.passwordConfirmTextField.text]) {
        [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"password_new_confirm_error", @"提示") completion:nil];
        return;
    }
    NSDictionary *param = @{
        @"oldPassword": self.oldPasswordTextField.text,
        @"password": self.passwordTextField.text
    };
    [BKIndicator showMessage:NSLocalizedString(@"submitting", @"提示")];
    [User resetPassword:param
               callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
        if (error) {
            [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
            return;
        }
        [BKIndicator showMessageAndFadeOut:NSLocalizedString(@"update_success", @"提示") completion:nil];
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
